//
//  FactoryIntroductionViewController.m
//  MagictoLane
//
//  Created by 锋 on 2017/12/7.
//  Copyright © 2017年 HUAYU. All rights reserved.
//

#import "FactoryIntroductionViewController.h"

@interface FactoryIntroductionViewController ()<UITextViewDelegate>

@end

@implementation FactoryIntroductionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    _introductionTextView.delegate = self;
    _placeholderLabel.text = _messageType == PRODUCEDETAILTYPE ? @"请输入生产说明" : @"请输入收货地址";
    if (_introductionStr.length > 0) {
        _introductionTextView.text = _introductionStr;
        _placeholderLabel.hidden = YES;
    }
    // Do any additional setup after loading the view from its nib.
}

- (void)setupUI {
    UIButton *ensureButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 38, 38)];
    [ensureButton setTitle:@"保存" forState:UIControlStateNormal];
    ensureButton.tintColor = [UIColor clearColor];
    [ensureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [ensureButton addTarget:self action:@selector(onClickEnsureBtn) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:ensureButton];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = _messageType == PRODUCEDETAILTYPE ? @"生产说明" : @"收货地址";
    self.view.backgroundColor = RGB(242, 242, 242);
//    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : [UIFont systemFontOfSize:19]};
}

- (void)onClickEnsureBtn {
    [self.introductionTextView resignFirstResponder];
    if (self.introductionTextView.text.length == 0) {
        [LTools showMBProgressWithText:_messageType == PRODUCEDETAILTYPE ? @"请输入生产说明" : @"请输入收货地址" addToView:sharedDelegate.window];
        return;
    }
    [self.delegate getFactoryIntroduction:self.introductionTextView.text andType:_messageType == PRODUCEDETAILTYPE ? @"PRODUCEDETAILTYPE" : @"ACCEPTAREATYPE"];
    [self.navigationController popViewControllerAnimated:YES];
}

// MARK: UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView
{
    if (textView.text.length > 0) {
        self.placeholderLabel.hidden = YES;
    } else {
        self.placeholderLabel.hidden = NO;
    }
    
    int count;
    count = (int)textView.text.length;
    [self.countLabel setText:[NSString stringWithFormat:@"%d/2000", count]];  //countLabel是一个显示剩余可输入数字的label
    
}

//限制textView输入字数
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text
{
    //这个判断相当于是textfield中的点击return的代理方法
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    
    //在输入过程中 判断加上输入的字符 是否超过限定字数
    NSString *str = [NSString stringWithFormat:@"%@%@", textView.text, text];
    if (str.length > 2000)
    {
        self.placeholderLabel.hidden = YES;
        textView.text = [str substringToIndex:2000];
        [self.countLabel setText:@"2000/2000"];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"输入的内容超过限制" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        
        return NO;
    }else{
        NSLog(@"%lu",(unsigned long)textView.text.length);
    }
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
